package com.newtouch.bxzs.enterprise.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.newtouch.bxzs.common.base.response.BaseResponse;
import com.newtouch.bxzs.common.base.response.RespGenerator;
import com.newtouch.bxzs.common.base.util.PinYinUtils;
import com.newtouch.bxzs.common.base.util.UserDetailUtil;
import com.newtouch.bxzs.enterprise.config.MyLog;
import com.newtouch.bxzs.enterprise.model.bo.CooperationUserBO;
import com.newtouch.bxzs.enterprise.model.bo.InitialsUserBO;
import com.newtouch.bxzs.enterprise.model.bo.PushCustomerRecordBO;
import com.newtouch.bxzs.enterprise.model.vo.CustomerCndVO;
import com.newtouch.bxzs.enterprise.model.vo.PushCustomerRecordVO;
import com.newtouch.bxzs.enterprise.service.PushCustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 客户推送Controller
 * <p>
 * <b>History:</b>
 * <table border="1">
 * <tr>
 * <th>Date</th>
 * <th>Operator</th>
 * <th>Memo</th>
 * </tr>
 * <tr>
 * <td>2021/1/13 9:41</td>
 * <td>Amour</td>
 * <td>Create</td>
 * </tr>
 * </table>
 *
 * @author Amour
 * @version 1.0.0
 * @since 1.0.0
 */
@Api(tags = "客户推送记录")
@RestController
@RequestMapping("push")
@SuppressWarnings("unchecked")
public class PushCustomerController {

	private static final Log logger = LogFactory.getLog(PushCustomerController.class);

	@Autowired
	private PushCustomerService pushCustomerService;

	@ApiOperation("创建客户推送记录")
	@PostMapping("/savePushRecord")
	@MyLog(systemFunctionName = "客户推送记录", systemFunctionUrl = "/push/savePushRecord", userOperationType = 1, userOperationSummary = "创建客户推送记录")
	public BaseResponse<String> savePushRecord(@RequestBody PushCustomerRecordVO pushCustomerRecordVO) {
		logger.info("执行PushCustomerController.savePushRecord():创建客户推送记录");
		BeanUtils.copyProperties(UserDetailUtil.getUserDetail(), pushCustomerRecordVO);
		return RespGenerator.returnOK(pushCustomerService.savePushRecord(pushCustomerRecordVO));
	}

	@ApiOperation("查询推送列表")
	@PostMapping("/getCustomerPushList")
	@MyLog(systemFunctionName = "客户推送记录", systemFunctionUrl = "/push/getCustomerPushList", userOperationType = 4, userOperationSummary = "查询推送列表")
	public BaseResponse<PushCustomerRecordBO> getCustomerPushList(@RequestBody CustomerCndVO customerCndVO) {
		logger.info("执行PushCustomerController.getCustomerPushList():获取推送列表");
		Page<PushCustomerRecordBO> customerPushList = pushCustomerService.getCustomerPushList(customerCndVO);
		return RespGenerator.returnOK(customerPushList);
	}

	@ApiOperation("撤回推送客户")
	@GetMapping("/revocationPush")
	@MyLog(systemFunctionName = "客户推送记录", systemFunctionUrl = "/push/revocationPush", userOperationType = 2, userOperationSummary = "撤回推送客户")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "pushCustomerRecordId", value = "客户推送记录id", type = "String", required = true)
	})
	public BaseResponse<String> revocationPush(@RequestParam("pushCustomerRecordId") String pushCustomerRecordId) {
		logger.info("执行PushCustomerController.getCustomerPushList():撤回推送客户");
		return RespGenerator.returnOK(pushCustomerService.revocationPush(pushCustomerRecordId));
	}

	@ApiOperation("根据三方关系获取对端用户信息")
	@PostMapping("/getReceiver")
	@MyLog(systemFunctionName = "客户推送记录", systemFunctionUrl = "/push/getReceiver", userOperationType = 4, userOperationSummary = "根据三方关系获取对端用户信息")
	public BaseResponse<Map<String, List<CooperationUserBO>>> getReceiver(@RequestParam("policyNumber") String policyNumber) {
		logger.info("执行PushCustomerController.getCustomerPushList():根据三方关系获取对端用户信息");
		CooperationUserBO cooperationUser = pushCustomerService.getgetReceiver(policyNumber);
		if(cooperationUser == null){
			return RespGenerator.returnOK(null);
		}
		List<CooperationUserBO> user = new ArrayList<>();
		user.add(cooperationUser);
		Map<String, List<CooperationUserBO>> initial = new HashMap<>(1);
		initial.put(
				PinYinUtils.toFirstChar(cooperationUser.getCooperationCompanyUserName().toUpperCase().substring(0, 1)),
				user);
		InitialsUserBO initialsUser = new InitialsUserBO(1, initial);
		return RespGenerator.returnOK(initialsUser);
	}

	/**
	 * 添加对端合作公司参数
	 */
	@PostConstruct
	public void addRedisCompanyUrl(){
		pushCustomerService.addRedisCompanyUrl();
	}
}
